#!usr/bin/python

#This module is used to update the tutorials. It downloads them from
#the compressed folder on the website and extracts them into the
#tutorials folder. It also updates the table of contents and this
#updater file to prevent need for future redistributions

import zipfile, urllib

UPDATE_SOURCE = 'http://codecadet.googlecode.com/files/codecadet.zip'

def update():
	"""Downloads and extracts the tutorials."""
	print "Downloading updates from: ", UPDATE_SOURCE
	try:
		filedata = urllib.urlretrieve(UPDATE_SOURCE)
	except:
		print "Could not find server. Update Failed"
		return
	print "Download complete. Extracting Files"
	zippedfolder = zipfile.ZipFile(filedata[0])
	for name in zippedfolder.namelist():
		print name
	zippedfolder.extractall()
	#~ tutorials = []
	#~ for item in zippedfolder.namelist():
		#~ if item[-4] == '.tut'
		#~ tutorials.append(item)
	#~ try:
		#~ tutorials.remove('table_of_contents.txt')
		#~ zippedfolder.extract('table_of_contents.txt')
	#~ except:
		#~ print "There is no table of contents found"
		#~ raise
	#~ try:
		#~ tutorials.remove('updater.py')
		#~ zippedfolder.extractall('tutorials',tutorials)
		#~ print "Updater updated"
	#~ except:
		#~ zippedfolder.extractall('tutorials',tutorials)
	#~ zippedfolder.close()
	#~ print "Update complete"
if __name__ == '__main__':
	update()
